package com.minelx.sqlstructures.statements.clause;

import java.util.List;

import static java.util.stream.Collectors.toList;

public class Clauses {
	public static Sql limit(int count) {
		return Sql.of("LIMIT " + count);
	}

	public static Sql orderBy(String field) {
		return Sql.of("ORDER BY " + field);
	}

	public static Sql groupBy(String fieldName) {
		return Sql.of("GROUP BY " + fieldName);
	}

	public static Sql where(List<Sql> conditions) {
		AndBuilder andBuilder = AndBuilder.create(conditions.stream().map(Sql::toText).collect(toList()));
		if (andBuilder.build().isEmpty()) {
			return Sql.EMPTY;
		}
		return Sql.of("WHERE " + andBuilder.build());
	}

	public static Sql select(List<Sql> clauses) {
		return () -> Sql.join(" ", clauses).toText() + ";";
	}
}
